package com.edu.exam.entity.assessment.technical;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

/**
 * 质量监控配置实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：quality_monitoring_config
 *
 * 根据E-06-03质量监控子模块设计实现
 * 对应quality_monitoring_config数据表，用于存储质量监控系统的配置参数
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class QualityMonitoringConfig {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "config_id", length = 32, unique = true, nullable = false)
    private String configId;

    @Column(name = "config_key", length = 100, unique = true, nullable = false)
    private String configKey;

    @Column(name = "config_value", columnDefinition = "JSON", nullable = false)
    private String configValue;

    @Column(name = "config_type", length = 50, nullable = false)
    private String configType;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "is_active", nullable = false)
    private Boolean isActive;

    @Column(name = "created_by", length = 32, nullable = false)
    private String createdBy;

    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    @Column(name = "updated_by", length = 32)
    private String updatedBy;

    @Column(name = "update_time")
    private LocalDateTime updateTime;

    // 扩展字段
    @Column(name = "category", length = 50)
    private String category; // 配置分类

    @Column(name = "data_type", length = 20)
    private String dataType; // 数据类型

    @Column(name = "validation_rule", columnDefinition = "TEXT")
    private String validationRule; // 验证规则

    @Column(name = "default_value", columnDefinition = "TEXT")
    private String defaultValue; // 默认值

    @Column(name = "environment", length = 20)
    private String environment; // 环境标识

    @Column(name = "version")
    private Integer version; // 版本号

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updateTime = now;

        // 设置默认值
        if (this.isActive == null) {
            this.isActive = true;
        }
        if (this.version == null) {
            this.version = 1;
        }
    }

    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
        if (this.version != null) {
            this.version = this.version + 1;
        }
    }

    /**
     * 检查配置是否有效
     */
}
